package day_2022_7_to_8.day48;
import java.util.*;
/**
 * @author haomin
 * @date 2022/08/28 19:09
 **/
public class Day43_5_Num98_IsValidBST {
    class Solution {
        List<Integer> list = new LinkedList<>();
        public boolean isValidBST(TreeNode root) {
            inorder(root);
            for(int i = 0; i < list.size()-1; ++i){
                if(list.get(i) >= list.get(i+1)){
                    return false;
                }
            }
            return true;
        }
        private void inorder(TreeNode root){
            if(root == null) return;

            inorder(root.left);
            list.add(root.val);
            inorder(root.right);
        }
    }
    class Solution2 {
        public boolean isValidBST(TreeNode root) {
            return inorder(root, Long.MIN_VALUE,Long.MAX_VALUE);
        }
        private boolean inorder(TreeNode root, long min, long max){
            if(root == null) return true;
            if(root.val <= min || root.val >= max) return false;

            return inorder(root.left, min, root.val) && inorder(root.right, root.val, max);
        }
    }
}